package com.wimcorp.magic.mana;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

import com.wimcorp.magic.color.Color;

@Embeddable
public class ManaHolder {

	@OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
	@JoinTable(name = "cost_manalist", joinColumns = @JoinColumn(name = "cost_id"), inverseJoinColumns = @JoinColumn(name = "manalist_id"))
	@OrderColumn
	private Map<Integer, ManaList> manaPool = new HashMap<Integer, ManaList>();

	public ManaHolder() {
	}

	protected ManaHolder(Color... colors) {
		for (Color color : colors) {
			addMana(color);
		}
	}

	public void addMana(Color color) {
		ManaList manaList = manaPool.get(color.ordinal());
		if (manaList == null) {
			manaList = new ManaList();
			manaPool.put(color.ordinal(), manaList);
		}
		manaList.getMana().add(new Mana(color));
	}

	public List<Mana> getMana(Color c) {
		ManaList manaList = manaPool.get(c.ordinal());
		if (manaList == null) {
			return new ArrayList<Mana>();
		}
		return manaList.getMana();
	}

	public Set<Color> getColorsInPool() {
		Set<Integer> keys = manaPool.keySet();
		Set<Color> colors = new HashSet<Color>();
		for (Integer key : keys) {
			colors.add(Color.values()[key]);
		}
		return colors;
	}
	
	public Collection<ManaList> getAllManaLists(){
		return manaPool.values();
	}

}
